/**
 * 
 */
package org.gov.vis.master.gui.listener;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.ResourceBundle;

import javax.swing.JOptionPane;

import org.gov.vis.common.exceptions.InvalidInputException;
import org.gov.vis.gui.util.ErrorsDisplayJPanel;
import org.gov.vis.main.Main;
import org.gov.vis.master.controller.StaffCtlr;
import org.gov.vis.master.controller.StaffCtlrImpl;
import org.gov.vis.master.dto.StaffDto;
import org.gov.vis.master.gui.EditStaffDialog;
import org.gov.vis.master.gui.ViewStaffDialog;
import org.gov.vis.util.GuiConstants;
import org.gov.vis.util.Util;

/**
 * @author user
 *
 */
public class EditStaffListener implements ActionListener, BaseListener{

	final private ResourceBundle rb = Util.getResources();

	private StaffCtlr staffCtlr = new StaffCtlrImpl();

	private EditStaffDialog editStaffDialog;

	private ErrorsDisplayJPanel errorMsgPanel;
	private StaffDto staffDto;

	public EditStaffListener(EditStaffDialog editStaffDialog) {
		// TODO Auto-generated constructor stub
		this.editStaffDialog = editStaffDialog;
	}

	@Override
	public void clear() {
		// TODO Auto-generated method stub

	}

	@Override
	public void setGuiDetailsToDto(Object _guiObject) {
		EditStaffDialog _editStaffDialog =(EditStaffDialog)_guiObject;
		staffDto = new StaffDto();
		staffDto.setCode(_editStaffDialog.getTxtFldCode().getText());
		staffDto.setDesignation(_editStaffDialog.getTxtFldDesignaton().getText());
		staffDto.setDistrict(_editStaffDialog.getTxtFldDistrict().getText());
		if(_editStaffDialog.getDtChoDateOfBirth().getDate() != null){
			staffDto.setDob(_editStaffDialog.getDtChoDateOfBirth().getDate());
		}
		if(_editStaffDialog.getDtChoDateOfJoin().getDate()!=null){
			staffDto.setDoj(_editStaffDialog.getDtChoDateOfJoin().getDate());
		}
		staffDto.setEmploymentType(String.valueOf(_editStaffDialog.getVisComBoxModel().getSelectedKey()));
		staffDto.setFatherName(_editStaffDialog.getTxtFldFatherName().getText());
		staffDto.setMandal(_editStaffDialog.getTxtFldMandal().getText());
		staffDto.setName(_editStaffDialog.getTxtFldName().getText());
		staffDto.setQualificatiion(_editStaffDialog.getTxtFldQualification().getText());
		staffDto.setVillage(_editStaffDialog.getTxtFldVillage().getText());
		staffDto.setStaffId((Long)_editStaffDialog.getHidnObject());

	}

	@Override
	public void setDtoDetailsToGui(Object _dtoObject) {
		// TODO Auto-generated method stub

	}

	@Override
	public void clearGUILabelColors(Color _labelColor) {
		// TODO Auto-generated method stub

	}

	@Override
	public void actionPerformed(ActionEvent actionEvent) {
		// TODO Auto-generated method stub

		errorMsgPanel = new ErrorsDisplayJPanel(editStaffDialog.getjPanel1());
		errorMsgPanel.clearMsgPanel();


		if(actionEvent.getActionCommand().equals(rb.getString("dialog_button_update"))){

			//Method called to either Add or Edit the Staff details
			addOrEditStaff();
		}

		if(actionEvent.getActionCommand().equals(rb.getString("dialog_button_delete"))){
			
			int response = JOptionPane.showConfirmDialog(null, GuiConstants.DELETE_CONFIRM_MSG, "Confirm",
			        JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
			    
		    if (response == JOptionPane.YES_OPTION) {
		      // User clicked YES.
		    	deleteStaff(actionEvent);
		    	String searchText = editStaffDialog.getSearchText();

				// It is basically doing back button functionality. Please, understand the logic and write the comments.
				//Replace double quotes with appropriate Dialog title.
				ViewStaffDialog viewStaffDialog = new ViewStaffDialog(Main.getMainFrame(), "View Staff", false);
				for(ActionListener actionListener: viewStaffDialog.getSearchBtn().getActionListeners()) { 
					ViewStaffListener viewStaffList = (ViewStaffListener)actionListener;
					viewStaffDialog.getCodeTxtBx().setText(searchText);

					viewStaffList.actionPerformed(new ActionEvent(viewStaffDialog.getSearchBtn(), 1, rb.getString("dialog_button_serach")));
				}

		    	
		    } else if (response == JOptionPane.NO_OPTION || response == JOptionPane.CLOSED_OPTION) {
		      return;
		    }

		}
	}

	private void addOrEditStaff() {

		//Method called to set the Field's Labels to default Black color
		clearGUILabelColors(blackColor);

		//Method to set the Screen's data to  the DTO Object
		setGuiDetailsToDto(editStaffDialog);

		try{
			//Method called to Add or Edit the Staff
			staffCtlr.addOrEditStaff(staffDto);
			JOptionPane.showMessageDialog(editStaffDialog, rb.getString(GuiConstants.MASTER_EDIT_STAFF_SUCCESS));
			editStaffDialog.dispose();	
			
			String searchText = editStaffDialog.getSearchText();

			// It is basically doing back button functionality. Please, understand the logic and write the comments.
			//Replace double quotes with appropriate Dialog title.
			ViewStaffDialog viewStaffDialog = new ViewStaffDialog(Main.getMainFrame(), "View Staff", false);
			for(ActionListener actionListener: viewStaffDialog.getSearchBtn().getActionListeners()) { 
				ViewStaffListener viewStaffList = (ViewStaffListener)actionListener;
				viewStaffDialog.getCodeTxtBx().setText(searchText);

				viewStaffList.actionPerformed(new ActionEvent(viewStaffDialog.getSearchBtn(), 1, rb.getString("dialog_button_serach")));
			}

		}catch(Exception e){
			List<String> errorMsgLst = new ArrayList<String>();
			if(e instanceof InvalidInputException){

				final HashMap<String,String> _exceptions = StaffCtlr.errorMsgs;
				if (_exceptions != null && !_exceptions.isEmpty()) {
					String errMsg = null;

					if(_exceptions.containsKey(rb.getString("dialog_label_staff_code").toUpperCase())){
						editStaffDialog.getLblCode().setForeground(redColor);
						errMsg = _exceptions.remove(rb.getString("dialog_label_staff_code").toUpperCase());
						errorMsgLst.add(errMsg);
					}

					if(_exceptions.containsKey(rb.getString("dialog_label_staff_name").toUpperCase())){
						editStaffDialog.getLblName().setForeground(redColor);
						errMsg = _exceptions.remove(rb.getString("dialog_label_staff_name").toUpperCase());
						errorMsgLst.add(errMsg);	
					}
					if(_exceptions.containsKey(rb.getString("dialog_label_staff_fathername").toUpperCase())){
						editStaffDialog.getLblFatherName().setForeground(redColor);
						errMsg = _exceptions.remove(rb.getString("dialog_label_staff_fathername").toUpperCase());
						errorMsgLst.add(errMsg);
					}

					if(_exceptions.containsKey(rb.getString("dialog_label_staff_dob").toUpperCase())){
						editStaffDialog.getLblDateOfBirth().setForeground(redColor);
						errMsg = _exceptions.remove(rb.getString("dialog_label_staff_dob").toUpperCase());
						errorMsgLst.add(errMsg);	
					}
					if(_exceptions.containsKey(rb.getString("dialog_label_staff_dofjoin").toUpperCase())){
						editStaffDialog.getLblDateOfJoin().setForeground(redColor);
						errMsg = _exceptions.remove(rb.getString("dialog_label_staff_dofjoin").toUpperCase());
						errorMsgLst.add(errMsg);
					}

					if(_exceptions.containsKey(rb.getString("dialog_label_staff_qualfcation").toUpperCase())){
						editStaffDialog.getLblQualification().setForeground(redColor);
						errMsg = _exceptions.remove(rb.getString("dialog_label_staff_qualfcation").toUpperCase());
						errorMsgLst.add(errMsg);	
					}
					if(_exceptions.containsKey(rb.getString("dialog_label_staff_designation").toUpperCase())){
						editStaffDialog.getLblDesignation().setForeground(redColor);
						errMsg = _exceptions.remove(rb.getString("dialog_label_staff_designation").toUpperCase());
						errorMsgLst.add(errMsg);
					}

					if(_exceptions.containsKey(rb.getString("dialog_label_staff_emptype").toUpperCase())){
						editStaffDialog.getLblEmpType().setForeground(redColor);
						errMsg = _exceptions.remove(rb.getString("dialog_label_staff_emptype").toUpperCase());
						errorMsgLst.add(errMsg);	
					}
					errorMsgPanel.setErrorMessages(errorMsgLst);
				} else {
					//log.debug(e.getMessage(), e);
				}	
			}
			else{
				errorMsgLst.add(e.getMessage());
				errorMsgPanel.setErrorMessages(errorMsgLst);

			}
		}


	}

	private void deleteStaff(ActionEvent actionEvent) {

		Long staffsId = (Long)editStaffDialog.getHidnObject();
		try {
			staffCtlr.deleteStaff(staffsId);
			JOptionPane.showMessageDialog(editStaffDialog, rb.getString(GuiConstants.MASTER_DELETE_STAFF_SUCCESS));
			editStaffDialog.dispose();
		} catch (Exception e) {

			List<String> errorMsgLst = new ArrayList<String>();
			errorMsgLst.add(e.getMessage());
			errorMsgPanel.setErrorMessages(errorMsgLst);

		}
	}



}
